package main.java.study.串口化通信.生化仪;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import org.json.JSONException;
import org.json.JSONObject;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/**
 * @Author 刘伟涛
 * @Date 2024/8/7 14:13
 **/
public class LISMockServer {

    public static void main(String[] args) throws IOException {
        // 创建HTTP服务器，监听端口8080
        HttpServer server = HttpServer.create(new InetSocketAddress(8120), 0);
        server.createContext("/", new LISHandler());
        server.setExecutor(null); // 使用默认的执行器
        server.start();
        System.out.println("LIS Mock Server is running on port 8120...");
    }

    static class LISHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            // 检查请求方法是否为POST
            if ("POST".equals(exchange.getRequestMethod())) {
                // 获取请求体内容
                InputStream inputStream = exchange.getRequestBody();
                ByteArrayOutputStream result = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int length;
                while ((length = inputStream.read(buffer)) != -1) {
                    result.write(buffer, 0, length);
                }
                String requestBodyString = result.toString("UTF-8");
                System.out.println("Received data: " + requestBodyString);
                String cleanRequestBodyString = requestBodyString.replace("\\", "\\\\"); // 转义反斜杠
                cleanRequestBodyString = cleanRequestBodyString.replace("\n", "")     // 移除换行符
                        .replace("\r", "") ;
                System.out.println(cleanRequestBodyString);


                JSONObject asJsonObject = null;
                try {
                    asJsonObject = new JSONObject(cleanRequestBodyString);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                study.仪器解析.串口化通信.生化仪.ASTMParser astmParser = new study.仪器解析.串口化通信.生化仪.ASTMParser();
                String msg = asJsonObject.getString("msg").replaceAll("\\s+", "");
                System.out.println("msg:" + msg);
                astmParser.parseASTMMessage(msg);


                // 设置响应内容
                String response = "{\"status\":\"success\",\"message\":\"Data received\"}";
                exchange.getResponseHeaders().set("Content-Type", "application/json; utf-8");
                exchange.sendResponseHeaders(200, response.length());

                // 发送响应
                OutputStream os = exchange.getResponseBody();
                os.write(response.getBytes());
                os.close();
            } else {
                exchange.sendResponseHeaders(405, -1); // Method Not Allowed
            }
        }
    }
}
